/*******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package gwt.simulation.client;

import java.io.Serializable;
import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GUIController implements EntryPoint
{   
    private final DbCommunicationServiceAsync dbCommSvc = GWT.create(DbCommunicationService.class);
	
	/**
     * This is the entry point method.
	 * @wbp.parser.entryPoint
     */
    public void onModuleLoad()
    {
    	GUIMaker ui = new GUIMaker();
    	ui.startParameters();
    	ui.createInterface();

    	
    	addBoxListener(ui);
    	
    }
    
	/**
	 * set listener to every box and manage this control between both sliders 
	 * @param ui 	object from user interface
	 * 
	 */
	public void addBoxListener(final GUIMaker ui) {
		ui.getSendButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				sendParametersToServer(ui);
			}
		});
		
		ui.getBoxBlockLength1().addValueChangeHandler(new ValueChangeHandler<Integer>() {
			public void onValueChange(ValueChangeEvent<Integer> event) {
				if(ui.getBoxBlockLength1().getValue() >= ui.getBoxBlockLength2().getValue() || ui.getBoxBlockLength1().getValue() < ui.getRangeSliderBlockLength().getMinimum()){
					ui.getBoxBlockLength1().setValue(ui.getRangeSliderBlockLength().getValueMin());
					ui.getRangeSliderBlockLength().setValues(ui.getBoxBlockLength1().getValue(), ui.getRangeSliderBlockLength().getValueMax());
				} else{
					ui.getRangeSliderBlockLength().setValues(ui.getBoxBlockLength1().getValue(), ui.getRangeSliderBlockLength().getValueMax());
				}
			}
		});
		
		
		ui.getBoxBlockLength2().addValueChangeHandler(new ValueChangeHandler<Integer>() {
			public void onValueChange(ValueChangeEvent<Integer> event) {
				if(ui.getBoxBlockLength2().getValue() <= ui.getBoxBlockLength1().getValue() || ui.getBoxBlockLength2().getValue() > ui.getRangeSliderBlockLength().getMaximum()){
					ui.getBoxBlockLength2().setValue(ui.getRangeSliderBlockLength().getValueMax());
					ui.getRangeSliderBlockLength().setValues(ui.getRangeSliderBlockLength().getValueMin(), ui.getBoxBlockLength2().getValue());
				} else {
					ui.getRangeSliderBlockLength().setValues(ui.getRangeSliderBlockLength().getValueMin(), ui.getBoxBlockLength2().getValue());
				}
			}
		});

		ui.getBoxCodeRate2().addValueChangeHandler(new ValueChangeHandler<Double>() {
			public void onValueChange(ValueChangeEvent<Double> event) {
				if(ui.getBoxCodeRate2().getValue() <= ui.getBoxCodeRate1().getValue() || ui.getBoxCodeRate2().getValue() > ui.getRangeSliderCodeRate().getMaximum()){
					ui.getBoxCodeRate2().setValue((double) ui.getRangeSliderCodeRate().getValueMax());
					ui.getRangeSliderCodeRate().setValues(ui.getRangeSliderCodeRate().getValueMin(), ui.getBoxCodeRate2().getValue().intValue());
				} else{
					ui.getRangeSliderCodeRate().setValues(ui.getRangeSliderCodeRate().getValueMin(), ui.getBoxCodeRate2().getValue().intValue());
				}			
			}
		});

		ui.getBoxCodeRate1().addValueChangeHandler(new ValueChangeHandler<Double>() {
			public void onValueChange(ValueChangeEvent<Double> event) {
				if(ui.getBoxCodeRate1().getValue() >= ui.getBoxCodeRate2().getValue() || ui.getBoxCodeRate1().getValue() < ui.getRangeSliderCodeRate().getMinimum()){
					ui.getBoxCodeRate1().setValue((double) ui.getRangeSliderCodeRate().getValueMin());
					ui.getRangeSliderCodeRate().setValues(ui.getBoxCodeRate1().getValue().intValue(), ui.getRangeSliderCodeRate().getValueMax());
				} else {
					ui.getRangeSliderCodeRate().setValues(ui.getBoxCodeRate1().getValue().intValue(), ui.getRangeSliderCodeRate().getValueMax());
				}
			}
		});
		
	} 
		
	/**
	 * method which calls and creates rpc service for server communication
	 * @param ui	object from user interface
	 * 
	 */
	public <E> void sendParametersToServer(final GUIMaker ui){
		
		JSONObject currentValue = new JSONObject();
		
		JSONString string = new JSONString(ui.getDropDownSimulationType().getItemText(ui.getDropDownSimulationType().getSelectedIndex()).toString());
		JSONNumber blockLength1 = new JSONNumber(ui.getBoxBlockLength1().getValue());
		JSONNumber blockLength2 = new JSONNumber(ui.getBoxBlockLength2().getValue());
		JSONNumber codeRate1 = new JSONNumber(ui.getBoxCodeRate1().getValue());
		JSONNumber codeRate2 = new JSONNumber(ui.getBoxCodeRate2().getValue());
		

		currentValue.put("simulation", string);
		currentValue.put("blockLengthMin", blockLength1);
		currentValue.put("blockLengthMax", blockLength2);	
		currentValue.put("codeRateMin", codeRate1);
		currentValue.put("codeRateMax", codeRate2);
		
		
		System.out.println(currentValue);
		

	    // Set up the callback object.
	    AsyncCallback<String> callback = new AsyncCallback<String>() {
	      public void onFailure(Throwable caught) {
	        Window.alert("falha");
	      }

	      public void onSuccess(String result) {
	        Window.alert(result);
	      }
	    };

	    // Make the call to the stock price service.
	    dbCommSvc.fetchData("testing", callback);
		
	    


			
	}			
}
	

